package com.fivefu.core.hik.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fivefu.core.hik.entity.DbHikRegionsCamera;
import com.fivefu.core.hik.mapper.DbHikRegionsCameraMapper;
import com.fivefu.core.hik.services.DbHikRegionsCameraService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 海康区域监控点关联表 服务实现类
 * </p>
 *
 * @author 
 * @since 2022-07-06 10:00:33
 */
@Service
public class DbHikRegionsCameraServiceImpl extends ServiceImpl<DbHikRegionsCameraMapper, DbHikRegionsCamera> implements DbHikRegionsCameraService {

    /**
     * 删除其他区域下关联监控点信息
     * @param notContainRegionIndexCodes 排除区域唯一标识
     */
    @Override
    public void deleteNotContainRegionsCameraByRegionIndexCodes(List<String> notContainRegionIndexCodes){
        QueryWrapper<DbHikRegionsCamera> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        if (!notContainRegionIndexCodes.isEmpty()) {
            wrapper.notIn("region_index_code", notContainRegionIndexCodes);
        }

        DbHikRegionsCamera camera = new DbHikRegionsCamera();
        camera.setIsDelete(1);
        camera.setUpdatedTime(LocalDateTime.now());
        //删除监控点关联
        baseMapper.update(camera, wrapper);
    }

    /**
     * 查询区域监控点Map集合
     * @return
     */
    @Override
    public Map<String, Map<String, Long>> queryRegionsCameraRegionMap(){
        QueryWrapper<DbHikRegionsCamera> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        wrapper.select("id,region_index_code,camera_index_code");
        
        List<DbHikRegionsCamera> list = baseMapper.selectList(wrapper);
        Map<String, Map<String, Long>> map = list.stream().collect(Collectors.groupingBy(DbHikRegionsCamera :: getRegionIndexCode, Collectors.toMap(DbHikRegionsCamera :: getCameraIndexCode, DbHikRegionsCamera::getId)));
        return map;
    }

    /**
     * 删除区域下监控点
     * @param regionIndexCode 区域唯一标识
     * @param regionCameraIDs 监控点唯一标识集合
     */
    @Override
    public void deleteRegionCameras(String regionIndexCode, List<Long> regionCameraIDs){
        QueryWrapper<DbHikRegionsCamera> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        wrapper.eq("region_index_code", regionIndexCode);
        wrapper.in("id", regionCameraIDs);

        DbHikRegionsCamera camera = new DbHikRegionsCamera();
        camera.setIsDelete(1);
        camera.setUpdatedTime(LocalDateTime.now());
        //删除监控点关联
        baseMapper.update(camera, wrapper);
    }

    /**
     * 批量增加区域监控点信息
     * @param regionIndexCode 区域唯一标识
     * @param cameraIndexCodeList 监控点唯一标识集合
     */
    @Override
    public void insertRegionCameras(String regionIndexCode, List<String> cameraIndexCodeList){
        LocalDateTime time = LocalDateTime.now();
        List<DbHikRegionsCamera> list = new ArrayList<>();
        for (String cameraIndexCode : cameraIndexCodeList) {
            DbHikRegionsCamera regionsCamera = new DbHikRegionsCamera();
            regionsCamera.setCameraIndexCode(cameraIndexCode);
            regionsCamera.setRegionIndexCode(regionIndexCode);
            regionsCamera.setCreatedTime(time);
            regionsCamera.setIsDelete(0);
            list.add(regionsCamera);
        }
        saveBatch(list);
    }

}
